package course.service.impl;

import course.dao.AdminDao;
import course.dao.impl.AdminDaoImpl;
import course.pojo.entity.Admin;
import course.service.AdminService;
import course.utils.JDBCUtil;

import java.sql.Connection;
import java.util.List;

/**
 * @author HLF
 * @date 2022/6/30 20:11
 */
public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminDaoImpl();

    @Override
    public Admin getById(Integer aid) {
        Connection connection = JDBCUtil.getConnection();
        Admin admin = adminDao.getById(aid, connection);
        JDBCUtil.closeConnection(connection);
        return admin;
    }

    @Override
    public int addAdmin(Admin admin) {
        Connection connection = JDBCUtil.getConnection();
        int i = adminDao.addAdmin(admin, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public int updateById(Admin admin) {
        Connection connection = JDBCUtil.getConnection();
        int i = adminDao.updateById(admin, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public int deleteById(Integer aid) {
        Connection connection = JDBCUtil.getConnection();
        int i = adminDao.deleteById(aid, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public List<Admin> getList() {
        Connection connection = JDBCUtil.getConnection();
        List<Admin> list = adminDao.getList(connection);
        JDBCUtil.closeConnection(connection);
        return list;
    }

    @Override
    public Admin login(Admin admin) {
        Connection connection = JDBCUtil.getConnection();
        Admin byAuser = adminDao.getByAuser(admin.getAuser(), connection);
        if (byAuser != null && byAuser.getApassword().equalsIgnoreCase(admin.getApassword())) {
            return byAuser;
        }
        JDBCUtil.closeConnection(connection);
        return null;
    }

    @Override
    public Admin getByAuser(String auser) {
        Connection connection = JDBCUtil.getConnection();
        Admin byAuser = adminDao.getByAuser(auser, connection);
        JDBCUtil.closeConnection(connection);
        return byAuser;
    }
}
